//package com.ruoyi.activiti.modeler.support;
//
//import org.activiti.engine.delegate.Expression;
//import org.activiti.engine.delegate.VariableScope;
//import org.activiti.engine.impl.bpmn.behavior.AbstractBpmnActivityBehavior;
//import org.activiti.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
//import org.activiti.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
//import org.activiti.engine.impl.bpmn.behavior.SequentialMultiInstanceBehavior;
//import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
//import org.activiti.engine.impl.pvm.process.ActivityImpl;
//import org.springframework.util.StringUtils;
//
//import java.util.Collection;
//import java.util.Collections;
//
//public class MultiInstanceBehaviorSupport {
//
//    @SuppressWarnings("serial")
//    public static class SequentialMultiInstanceBehaviorSupport extends SequentialMultiInstanceBehavior {
//
//        public SequentialMultiInstanceBehaviorSupport(ActivityImpl activity,
//                                                      AbstractBpmnActivityBehavior innerActivityBehavior) {
//            super(activity, innerActivityBehavior);
//            this.setCollectionExpression(new CollectionExpression(this));
//        }
//
//        @Override
//        public void execute(ActivityExecution execution) throws Exception {
//            if (innerActivityBehavior instanceof UserTaskActivityBehaviorSupport
//                    && getLocalLoopVariable(execution, getCollectionElementIndexVariable()) == null) {
//                ((UserTaskActivityBehaviorSupport) innerActivityBehavior).beforeExecuteMultiInstance(execution);
//            }
//            super.execute(execution);
//        }
//
//        @Override
//        public void completed(ActivityExecution execution) throws Exception {
//            LOGGER.info("SequentialMultiInstanceBehavior.completed->processId:{}, task:{}",execution.getProcessInstanceId(), activity.getId());
//            execution.removeVariable(getCollectionElementIndexVariable());
//            leave(execution);
//        }
//    }
//
//    @SuppressWarnings("serial")
//    public static class ParallelMultiInstanceBehaviorSupport extends ParallelMultiInstanceBehavior {
//
//        public ParallelMultiInstanceBehaviorSupport(ActivityImpl activity,
//                                                    AbstractBpmnActivityBehavior originalActivityBehavior) {
//            super(activity, originalActivityBehavior);
//            this.setCollectionExpression(new CollectionExpression(this));
//        }
//
//        @Override
//        public void execute(ActivityExecution execution) throws Exception {
//            if (innerActivityBehavior instanceof UserTaskActivityBehaviorSupport
//                    && getLocalLoopVariable(execution, getCollectionElementIndexVariable()) == null) {
//                ((UserTaskActivityBehaviorSupport) innerActivityBehavior).beforeExecuteMultiInstance(execution);
//            }
//            super.execute(execution);
//        }
//        @Override
//        public void completed(ActivityExecution execution) throws Exception {
//            LOGGER.info("ParallelMultiInstanceBehavior.completed->processId:{}, task:{}",execution.getProcessInstanceId(), activity.getId());
//            execution.removeVariable(getCollectionElementIndexVariable());
//            leave(execution);
//        }
//    }
//
//    @SuppressWarnings("serial")
//    static class CollectionExpression implements Expression {
//        private MultiInstanceActivityBehavior behavior;
//
//        public CollectionExpression(MultiInstanceActivityBehavior behavior) {
//            this.behavior = behavior;
//        }
//
//        @Override
//        public Object getValue(VariableScope variableScope) {
//            String name = this.getExpressionText();
//            Object value = variableScope.getVariable(name);
//            if(StringUtils.isEmpty(value)) {
//                return Collections.emptyList();
//            } else if(value instanceof Collection) {
//                return (Collection<?>)value;
//            }
////            return Utils.splitToList(value.toString(), ",");
//            return "";
//        }
//
//        @Override
//        public void setValue(Object value, VariableScope variableScope) {
//            //nothing to do
//        }
//
//        @Override
//        public String getExpressionText() {
//            return this.behavior.getCollectionVariable();
//        }
//    }
//}
